<?php

  /**
   * Template implements methods to parse and display (x)html templates.<br />
   * Store the filename and the content of the template in private.<br />
   * @param string $filename The name of the template without extension.
   *
   * @author Dany Mottier
   * @version 2.3
   */
  class Template extends Base {

    /** @var string The path to all template files. */
    private $path = "templates/";

    /** @var string The extension for all template files. */
    private $extension = ".tmpl";

    /** @var string The complete (x)html source of the template. */
    private $source = "";

    /** @var string The template filename. */
    private $filename = "";

    /** @var string The delimiter for each key in the source. */
    private $delimiter = "{.}";

    /** @var string The delimiter for each label to translate in the source. */
    private $labelDelimiter = "[.]";

    /**
     * Store the filename and the content of the template in private.<br />
     * @param string $filename The name of the template.
     */
    public function __construct($filename, $path = "templates/") {
      $this->path = $path;

      $this->filename = $this->path . $filename . $this->extension;

      if (file_exists($this->filename)) {
        $this->source = file_get_contents($this->path .
                $filename .
                $this->extension);
      }
      else
        throw new FileNotFoundException("File does not exist");
    }

    /**
     * Returns the extension for all template files.<br />
     * @return string Returns the extension.
     */
    public function getExtension() {
      return $this->extension;
    }

    /**
     * Returns the path to all template files.<br />
     * @return string Returns the path.
     */
    public function getPath() {
      return $this->path;
    }

    /**
     * Returns the (x)html source.<br />
     * @return string Returns the (x)html source.
     */
    public function getSource() {
      return $this->source;
    }

    /** Display the source. */
    public function display() {
      print $this->source;
    }

    /**
     * Replace the key by the value in the source.<br />
     * @param string $key The key to replace.
     * @param string $value The value to set in place of the key.
     * @see <a href="http://fr2.php.net/manual/en/function.eregi-replace.php">eregi_replace()</a>
     */
    public function setValue($key, $value) {
      $pattern = str_ireplace(".", $key, $this->delimiter);
      $this->source = preg_replace("@" . $pattern . "@i", $value, $this->source);
    }

    /**
     * Replaces the key label by the value in the source.<br />
     *
     * @param string $key The label to replace.
     * @param string $value The value of the label to be replaced.
     *
     * @see <a href="http://be.php.net/manual/en/function.is-string.php">is_string()</a>
     * @see <a href="http://be.php.net/manual/en/function.empty.php">empty()</a>
     * @see IllegalArgumentException
     */
    public function setLabel($key, $value) {
      if (is_string($key) && !empty($key) && is_string($value) && !empty($value)) {
        $pattern = str_replace(".", $key, $this->labelDelimiter);

        $this->source = str_replace($pattern, $value, $this->source);
      }
      else
        throw new IllegalArgumentException("\$key and \$value parameters must be strings.");
    }

  }

?>
